<?php

class Admin_PermissaoController extends App_Controller_Action
{
    private $_table     = 'PERMISSAO';
    private $_pk        = 'PER_ID';
    private $_order_col = 'PER_NOME';
    private $_order     = 'ASC';
    private $_sta_col   = 'PER_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'PER_FILE'; 
    public  $dir        = 'public/upload/';
    
    public function init()
    {
        //if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Permissao();
    }

    public function indexAction() {        
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
    
    public function addAction() {
        $this->_helper->viewRenderer('edit');
        
        $id = $this->_getParam('id', 0);
        
        $this->view->PER_ID = $id;
        
    }        
    
    public function editAction() {
        
        Zend_Loader::loadClass('dominio','library');
        
        $id = $this->_getParam('id', 0);
        
        $this->form = new Admin_Form_Permissao(null, $id);
        
        $this->view->form = $this->form;
        
        $this->view->PER_ID = $id;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $arr_ck = $formData['MOD_ID'];
            
            if ($this->form->isValid($formData)) {
                $id = (int) $this->form->getValue('PER_ID');
                $data = array(
                    'PER_ID'     => $this->form->getValue('PER_ID'),
                    'PER_NOME'   => $this->form->getValue('PER_NOME'),
                    'PER_STA'    => $formData['PER_STA'],
                    'PER_STAIPA' => $formData['PER_STAIPA'],
                );
                
                if ($id == 0){
                    $this->db->insert($this->_table,$data);
                    $id = $this->db->lastInsertId();
                }
                else{
                    $this->db->update($this->_table,$data, $this->_pk.' = ' . $id);
                }
                
                $this->db->delete('PERMISSAO_HAS_MODULO',$this->_pk . ' = ' . $id);
                foreach($arr_ck as $ck){
                    $data_permissao = array(
                        'PER_ID' => $id,
                        'MOD_ID' => $ck,
                        'MOD_ADD' => isset($formData['MOD_ADD'][$ck]) ? $formData['MOD_ADD'][$ck]:0,
                        'MOD_EDT' => isset($formData['MOD_EDT'][$ck]) ? $formData['MOD_EDT'][$ck]:0,
                        'MOD_DEL' => isset($formData['MOD_DEL'][$ck]) ? $formData['MOD_DEL'][$ck]:0,
                    );
                    $this->db->insert('PERMISSAO_HAS_MODULO',$data_permissao);
                }
               $this->_helper->redirector('index');
            } else {
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk .' = ' . $id;
                $result = $this->db->fetchRow($query);
                $this->form->populate($result);
                $this->view->obj = $result;
            }
        }
    }

    public function deleteAction() {
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            
            $data = array($this->_sta_col => 0);
            $ok = (bool) $this->db->update($this->_table,$data, $this->_pk .' = ' . $id);
            
            $this->_helper->redirector('index');
        }
    }
    
    public function editipAction() {
        
        $this->view->titulo = $this->titulo.' - IP';
        
        $id = $this->_getParam('id', 0);
        $phi_id = $this->_getParam('phi_id', 0);
        $op = $this->_getParam('op', '');
        
        $this->view->id = $id;
        
         if($id == 0) {
            $urlOptions = array('controller'=>'permissao','action'=>'index','id'=>'','op'=>'');
            $this->_helper->redirector->gotoRoute($urlOptions);
         }
        
        if($op == ''){
            $this->_helper->viewRenderer->setRender('indexip');
            
            $query = 'SELECT SQL_CACHE * FROM PERMISSAO_HAS_IP WHERE '.$this->_pk . ' = ' . $id;
            $data = $this->db->fetchAll($query);
            $this->view->result = $data;
            
        } else if($op == 'edit'){
            $this->_helper->viewRenderer->setRender('editip');
            
            if($this->getRequest()->isPost()) {
                $formData = $this->getRequest()->getPost();
                $data = $formData;
                
                unset($data['submit']);
                $data[$this->_pk] = $formData[$this->_pk];
                $data['PHI_IP'] = $formData['PHI_IP'];
                
                if($formData['PHI_ID'] == 0)
                    $this->db->insert('PERMISSAO_HAS_IP',$data);
                else
                    $this->db->update('PERMISSAO_HAS_IP',$data,'PHI_ID = '.$formData['PHI_ID']);
                
                $urlOptions = array('controller'=>$this->_controller,'action'=>'editip','id'=>$id,'op'=>'');
                $this->_helper->redirector->gotoRoute($urlOptions);
            }
            else if($phi_id > 0){
                $query = 'SELECT SQL_CACHE * FROM PERMISSAO_HAS_IP WHERE PHI_ID = ' . $phi_id;
                $data = $this->db->fetchRow($query);
                $this->view->data = $data;
            }
                
        }else if($op == 'delete'){
            
            $ok = (bool) $this->db->delete('PERMISSAO_HAS_IP', 'PHI_ID = ' . $phi_id );
        
            $urlOptions = array('controller'=>$this->_controller,'action'=>'editip','id'=>$id,'op'=>'');
            $this->_helper->redirector->gotoRoute($urlOptions);
         
        }
        
        
        
    }

   



}